import { reg } from 'services/home';
import * as sealService from 'services/sealService';
import * as signatureService from 'services/signatureService';
import { Toast } from 'antd-mobile';
export default {
  namespace: 'sealAndSignatureModel',
  state: {
    sealList: [],
    signatureList: [],
    page: {
      totalElements: 0
    }
  },
  effects: {
    *submitConfirm({ payload, callback }, { call, put }) {
      // Toast.loading('加载中!',0)
      const response = yield call(sealService.postSubmitConfirm, payload);
      if (response.code.substring(response.code.length - 4) === '0000') {
        Toast.hide();
        callback(1)
        // Toast.success('确认签署成功！');
      } else {
        Toast.fail('确认签署失败！');
        callback(0);
      }
    },

    *getFormsSearch({ payload, callback }, { call, put }) {
      const response = yield call(sealService.getFormsSearch, payload);
      if (response.code.substring(response.code.length - 4) === '0000') {
        yield put({
          type: 'saveForms',
          payload: response.forms
        });
        callback(response.forms);
        // Toast.success('查询表单成功!')
      } else {
        Toast.fail('查询表单失败!')
      }
    },
    *getSealList({ payload, callback, refresh }, { call, put }) {
      const response = yield call(sealService.getSealList, payload);
      if (response.code.substring(response.code.length - 4) == '0000') {
        const { officeSeals, page } = response;
        yield put({
          type: refresh ? 'save' : 'saveSealList',
          payload: {
            sealList: officeSeals,
            defaultList: officeSeals,
            page: page,
          },
        });
        if (callback && typeof callback === 'function') {
          callback();
        }
      }
      else {
        Toast.fail("获取印章列表失败！")
      }
    },
    *getSignatureList({ payload, callback }, { call, put }) {
      const response = yield call(signatureService.getSignatureList, payload);
      if (response.code.substring(response.code.length - 4) == '0000') {
        yield put({
          type: 'save',
          payload: {
            signatureList: response.personSeals
          }
        });
        if (callback && typeof callback === 'function') {
          callback(response.personSeals);
        }
      }
      else {
        Toast.fail("获取签名列表失败！")
      }
    },
    *postSignatureAdd({ payload, callback }, { call, put }) {
      const response = yield call(signatureService.postSignatureAdd, payload);
      if (response.code.substring(response.code.length - 4) !== '0000') {
        Toast.fail('上传签名失败！')
      }
      else {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
      }
    },
    *delSignatureDelete({ payload }, { call, put }) {
      const response = yield call(signatureService.delSignatureDelete, payload);
      if (response.code.substring(response.code.length - 4) !== '0000') {
        Toast.fail('删除签名失败！')
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
    saveSealList(state, { payload }) {
      return {
        ...state,
        sealList: [
          ...state.sealList,
          ...payload.sealList,
        ],
        page: payload.page
      }
    },
  },

};
